<?php

namespace app\wechat\lib;
use think\Config;
use think\Cache;

class AccessToken
{
    // https请求方式: GET
    // https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET
    private static function init()
    {
        $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='. Config::get('appid') . '&secret=' . Config::get('appsecret');
        $res = https_curl($url);
        $res = json_decode($res,true);
        return $res;
    }

    public static function get()
    {
        $accessToken = Cache::get('access_token');
        if ( !$accessToken ) {
            $res = self::init();
            Cache::set('access_token',$res['access_token'],$res['expires_in'] - 200);
            $accessToken = $res['access_token'];
        }
        return $accessToken;
    }
}
